import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

class User{
    public String username;
    public String password;
}
@WebServlet("/json")
public class jsonservlet extends HttpServlet {
    //此处约定客户端body按照json格式来进行传输
    //其中格式形如：
    //{
    //   username:"zhangsan",
    //   password:"123"
    //}

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            ObjectMapper mapper = new ObjectMapper();
            User user = mapper.readValue(req.getInputStream(), User.class);
            System.out.println("username: " + user.username+", password: "+user.password);
            String json = mapper.writeValueAsString(user);
            System.out.println("json: " + json);
            resp.getWriter().write("ok");
    }
}
